﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BankApplication.BusinessLogic;
using System.ServiceModel;
using BusinessLogic.BusinessEntity.Entities;

namespace BankApplication.UI
{
	public partial class AccountForm : Form
	{
		public AccountForm()
		{
			InitializeComponent();
		}

		private void ValidateForm()
		{
			int accountNumber;
			decimal initialBalance;

			try
			{
				accountNumber = Convert.ToInt32(AccountNumberTextBox.Text);
			}
			catch (Exception)
			{
				throw new ValidationException(String.Format("Incorrect account number: {0}!" +
					" The account number must be a valid positive integer value less  than or equal to {1}!" , AccountNumberTextBox.Text, Int32.MaxValue));
			}

			try
			{
				initialBalance = Convert.ToDecimal(BalanceTextBox.Text);
			}
			catch (Exception)
			{
				throw new ValidationException(String.Format("Incorrect initial balance: {0}!" +
					" Initial balance must be a valid decimal value! (e.g. 100,34)", BalanceTextBox.Text));
			}

			
			if (initialBalance < 0)
			{
				throw new ValidationException("The initial balance cannot be negative!");
			}

			if (accountNumber <= 0)
			{
				throw new ValidationException("The account number must be a valid positive integer value less  than or equal to " + Int32.MaxValue);
			}


			if (CurrenciesComboBox.SelectedIndex == -1)
			{
				throw new ValidationException("The currency is not selected!");
			}

			if (AccounteeCombobox.SelectedIndex == -1)
			{
				throw new ValidationException("The accountee isn't selected!");
			}
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			try
			{
				ValidateForm();
			
				Account account = new Account();

				account.Number = AccountNumberTextBox.Text;
				account.Balance = Convert.ToDecimal(BalanceTextBox.Text);

				account.Currency = currencies[CurrenciesComboBox.SelectedIndex];
				account.Accountee = users[AccounteeCombobox.SelectedIndex];

				new ServerInteractionExceptionCatcher().Execute(() =>
				{
					AccountsManager.CreateAccount(account, SessionManager.Instance.SessionTicket);

					Close();
				});

				
			}
			catch (ValidationException exception)
			{
				MessageBox.Show(exception.Message, "Form validation failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void CloseButton_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void AccounteeCombobox_DropDown(object sender, EventArgs e)
		{
			new ServerInteractionExceptionCatcher().Execute(() =>
				{
					users = UsersManager.GetAllUsers(SessionManager.Instance.SessionTicket);
					var userLogins = users.Select(user => user.Login);
					AccounteeCombobox.DataSource = userLogins.ToList();
				});
		}

		private void CurrenciesComboBox_DropDown(object sender, EventArgs e)
		{
			new ServerInteractionExceptionCatcher().Execute(() =>
				{
					currencies = CurrenciesManager.GetCurrencies(SessionManager.Instance.SessionTicket);
					var currencyNames = currencies.Select(currency => currency.Name).ToList();
					CurrenciesComboBox.DataSource = currencyNames;
				});
		}


		private IList<Currency> currencies;
		private IList<User> users;
	}
}
